package lib.quasar.widget.menu;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import lib.quasar.util.DimenUtil;
import lib.quasar.util.LogUtil;
import lib.quasar.widget.R;
import lib.quasar.widget.paint.PaintManager;

public class NextView extends View {

    private final Path mPath = new Path();
    private float arrowWidth = -1f;
    private float arrowHeight = -1f;
    private float lineSize = DimenUtil.dp2px(2f);

    private float textSize = DimenUtil.sp2px(16f);

    private String menuText;
    private float menuWidth = 0f;

    private String hintText;
    private int hintTextColor = Color.GRAY;

    private String messageText;
    private int messageTextColor = Color.BLACK;

    public NextView(Context context) {
        super(context);
    }

    public NextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        TypedArray typed = null;
        try {
            typed = context.obtainStyledAttributes(attrs, R.styleable.NextView, 0, 0);
            arrowWidth = typed.getDimension(R.styleable.NextView_nv_arrow_width, arrowWidth);
            arrowHeight = typed.getDimension(R.styleable.NextView_nv_arrow_height, arrowHeight);
            lineSize = typed.getDimension(R.styleable.NextView_nv_line_size, lineSize);
            textSize = typed.getDimension(R.styleable.NextView_nv_text_size, textSize);
            menuText = typed.getString(R.styleable.NextView_nv_menu_text);
            menuWidth = typed.getDimension(R.styleable.NextView_nv_menu_width, menuWidth);
            hintText = typed.getString(R.styleable.NextView_nv_hint_text);
            hintTextColor = typed.getColor(R.styleable.NextView_nv_hint_text_color, hintTextColor);
            messageTextColor = typed.getColor(R.styleable.NextView_nv_message_text_color, messageTextColor);
        } catch (Exception e) {
            LogUtil.e("", e.getMessage(), e);
        } finally {
            if (null == typed) return;
            typed.recycle();
        }
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        final int width = getWidth();
        final int height = getHeight();
        final int paddingRight = getPaddingRight();

        // 箭头
        if (arrowWidth != -1f && arrowHeight != -1f) {

            final Paint linePaint = PaintManager.getLinePaint(Color.GRAY, lineSize);
            mPath.moveTo(width - paddingRight - arrowWidth, height * 0.5f - arrowHeight * 0.5f);
            mPath.lineTo(width - paddingRight, height * 0.5f);
            mPath.lineTo(width - paddingRight - arrowWidth, height * 0.5f + arrowHeight * 0.5f);
            canvas.drawPath(mPath, linePaint);
        }

        // 菜单文字
        if (menuWidth >0f && !TextUtils.isEmpty(menuText)) {
            final Paint textPaint = PaintManager.getTextPaint(Color.BLACK, textSize);
            final Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
            final float font = (fontMetrics.bottom - fontMetrics.top) * 0.3f;
            canvas.drawText(menuText, menuWidth*0.5f, height * 0.5f + font, textPaint);
        }

        if (!TextUtils.isEmpty(messageText)) {

            final Paint textPaint = PaintManager.getTextPaint(messageTextColor, textSize);
            textPaint.setTextAlign(Paint.Align.LEFT);
            final Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
            final float font = (fontMetrics.bottom - fontMetrics.top) * 0.3f;
            canvas.drawText(messageText.toString(), menuWidth + getPaddingLeft(), height * 0.5f + font, textPaint);

        } else if (!TextUtils.isEmpty(hintText)) {

            final Paint textPaint = PaintManager.getTextPaint(hintTextColor, textSize);
            textPaint.setTextAlign(Paint.Align.LEFT);
            final Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
            final float font = (fontMetrics.bottom - fontMetrics.top) * 0.3f;
            canvas.drawText(hintText, menuWidth + getPaddingLeft(), height * 0.5f + font, textPaint);
        }

        final Paint linePaint = PaintManager.getLinePaint(getResources().getColor(R.color.color_line_gray), lineSize);
        linePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.drawLine(0f, height, width, height, linePaint);
    }

    public void setText(String str) {
        if (TextUtils.isEmpty(str)) return;
        messageText = str;
        postInvalidate();
    }

    public void clearText() {
        messageText = "";
        postInvalidate();
    }

    public String getText() {
        return messageText;
    }
}
